package com.jamie.jvm.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * CAS是什么？ ===> compareAndSet
 * atomicInteger.compareAndSet("期望值，更新值")
 * 比较并交换
 */
public class CASDemo {

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(5);
        System.out.println("update result: "
                + atomicInteger.compareAndSet(5, 2019)
                + "\t current data: "
                + atomicInteger.get());
        // 如果期望值，不等于当前值，则修改失败
        System.out.println("update result: "
                + atomicInteger.compareAndSet(5, 1024)
                + "\t current data: "
                + atomicInteger.get());
    }

}
